import db from "../models/index.js";
import {Op} from "sequelize";

let msg03 = db.msg_03;
let msg03Dao = {};
//获取某个sessionID的聊天记录
msg03Dao.getMessageListBySessionId = (_session_id, startTimestamp = 0) => {
  return msg03.findAndCountAll({
    where: {
      _session_id,
      _update_time: {[Op.gt]: startTimestamp},
    },
    raw: true,
  });
};
//添加某个sessionID的聊天记录
msg03Dao.addUserMsg = (_session_id, _sender, _timestamp, _message, _status, _update_time, t) => {
  if (!t) {
    return msg03.create({_session_id, _sender, _timestamp, _message, _status, _update_time});
  }
  return msg03.create({_session_id, _sender, _timestamp, _message, _status, _update_time}, {transaction: t});
};
//更新某个sessionID的聊天记录 比如撤回之类的
msg03Dao.updateUserMsg = (_session_id, _sender, _timestamp, _message, _status, _update_time, t) => {
  if (!t) {
    return msg03.update({_session_id, _sender, _timestamp, _message, _status, _update_time}, {
      where: {
        _session_id,
        _sender,
        _timestamp
      }
    });
  }
  return msg03.update({_session_id, _sender, _timestamp, _message, _status, _update_time}, {
    where: {
      _session_id,
      _sender,
      _timestamp
    }, transaction: t
  });
};
export default msg03Dao;
